package com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyRegion.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyRegion.repository.HddcWyGeomorphysvyregionNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyRegion.repository.HddcWyGeomorphysvyregionRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyRegion.repository.entity.HddcWyGeomorphysvyregionEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyRegion.service.HddcWyGeomorphysvyregionService;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyRegion.viewobjects.HddcWyGeomorphysvyregionQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyRegion.viewobjects.HddcWyGeomorphysvyregionVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lihelei
 * @date 2020-12-01
 */
@Service
public class HddcWyGeomorphysvyregionServiceImpl implements HddcWyGeomorphysvyregionService {

	@Autowired
    private HddcWyGeomorphysvyregionRepository hddcWyGeomorphysvyregionRepository;
    @Autowired
    private HddcWyGeomorphysvyregionNativeRepository hddcWyGeomorphysvyregionNativeRepository;

    @Override
    public JSONObject queryHddcWyGeomorphysvyregions(HddcWyGeomorphysvyregionQueryParams queryParams , int curPage,
                                                     int pageSize) {
        Page<HddcWyGeomorphysvyregionEntity> hddcWyGeomorphysvyregionPage =
                this.hddcWyGeomorphysvyregionNativeRepository.queryHddcWyGeomorphysvyregions(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyGeomorphysvyregionPage);
        return jsonObject;
    }


    @Override
    public HddcWyGeomorphysvyregionEntity getHddcWyGeomorphysvyregion(String id) {
        HddcWyGeomorphysvyregionEntity hddcWyGeomorphysvyregion = this.hddcWyGeomorphysvyregionRepository.findById(id).orElse(null);
         return hddcWyGeomorphysvyregion;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeomorphysvyregionEntity saveHddcWyGeomorphysvyregion(HddcWyGeomorphysvyregionEntity hddcWyGeomorphysvyregion) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyGeomorphysvyregion.setId(uuid);
        hddcWyGeomorphysvyregion.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyGeomorphysvyregion.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeomorphysvyregionRepository.save(hddcWyGeomorphysvyregion);
        return hddcWyGeomorphysvyregion;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeomorphysvyregionEntity updateHddcWyGeomorphysvyregion(HddcWyGeomorphysvyregionEntity hddcWyGeomorphysvyregion) {
        hddcWyGeomorphysvyregion.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyGeomorphysvyregion.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeomorphysvyregionRepository.save(hddcWyGeomorphysvyregion);
        return hddcWyGeomorphysvyregion;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyGeomorphysvyregions(List<String> ids) {
        List<HddcWyGeomorphysvyregionEntity> hddcWyGeomorphysvyregionList = this.hddcWyGeomorphysvyregionRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyGeomorphysvyregionList) && hddcWyGeomorphysvyregionList.size() > 0) {
            for(HddcWyGeomorphysvyregionEntity hddcWyGeomorphysvyregion : hddcWyGeomorphysvyregionList) {
                this.hddcWyGeomorphysvyregionRepository.delete(hddcWyGeomorphysvyregion);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public BigInteger queryHddcWyGeomorphysvyregion(HddcWyGeomorphysvyregionQueryParams queryParams) {
        BigInteger bigInteger = hddcWyGeomorphysvyregionNativeRepository.queryHddcWyGeomorphysvyregion(queryParams);
        return bigInteger;
    }

}
